#include "vlaunchwidget.h"
#include <QPainter>
#include <QPainterPath>
#include <QPaintEvent>
#include "vloginwidget.h"
#include "vclosebutton.h"
#include <QLabel>
#include "vregistwidget.h"
#include "vrevisewidget.h"
#include "vswitchinterfacebutton.h"
VLaunchWidget::VLaunchWidget(QWidget* parent)
    : QWidget(parent)
    , m_pButtonClose(new VCloseButton(this))//关闭按钮
    , m_pLabelTitle(new QLabel(this))//标签
    , m_pLoginWidget(new VLoginWidget(this))
    , m_pRegistWidget(new VRegistWidget(this))
    , m_pReviseWidget(new VReviseWidget(this))
{
	resize(280, 380);
	setWindowFlag(Qt::FramelessWindowHint);//无边框
	setAttribute(Qt::WA_TranslucentBackground);
    setMainWidget(m_pLoginWidget);

    initLabelTitle();
    installEventFilter(this);//安装事件过滤器
    initConnect();
}

void VLaunchWidget::initConnect()
{
    (void)connect(m_pLoginWidget->m_pButtonSwitchLoginWidget, &VSwitchInterfaceButton::released, this, &VLaunchWidget::onVSwitchInterfaceButtonReleased);
    (void)connect(m_pLoginWidget->m_pButtonSwitchReviseWidget, &VSwitchInterfaceButton::released, this, &VLaunchWidget::onVSwitchInterfaceButtonReleased);
    (void)connect(m_pRegistWidget->m_pButtonSwichBack, &VSwitchInterfaceButton::released, this, &VLaunchWidget::onVSwitchInterfaceButtonReleased);
    (void)connect(m_pReviseWidget->m_pButtonSwichBack, &VSwitchInterfaceButton::released, this, &VLaunchWidget::onVSwitchInterfaceButtonReleased);
}

VLaunchWidget::~VLaunchWidget()
{
}

void VLaunchWidget::onVSwitchInterfaceButtonReleased()
{
         if (sender() == m_pLoginWidget->m_pButtonSwitchLoginWidget)    setMainWidget(m_pRegistWidget);
    else if (sender() == m_pLoginWidget->m_pButtonSwitchReviseWidget)   setMainWidget(m_pReviseWidget);
    else if (sender() == m_pRegistWidget->m_pButtonSwichBack)      setMainWidget(m_pLoginWidget);
    else if (sender() == m_pReviseWidget->m_pButtonSwichBack)      setMainWidget(m_pLoginWidget);
}
void VLaunchWidget::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.fillRect(event->rect(), Qt::transparent);
    QPainterPath path;
    static int radius = 4;
    path.addRoundedRect(this->rect(), radius, radius);
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    painter.fillPath(path, QColor("#FFFFFF")); // 使用不透明的白色
    painter.setPen(QPen(QBrush(QColor("#DADADA")), 1)); // 设置边框颜色和粗细
    painter.drawPath(path);
}


bool VLaunchWidget::eventFilter(QObject* obj, QEvent* event)
{
    static QPoint s_pressPos;
    static QPoint s_widgetPos;
    switch (event->type())
    {
    case QEvent::MouseButtonPress:
        s_pressPos = QCursor::pos();//获取鼠标位置
        s_widgetPos = pos();//获取控件的位置
        break;
    case QEvent::MouseButtonRelease:
        break;
    case QEvent::MouseMove:
        if ((QCursor::pos() - pos()).y() <= 25)
            move(s_widgetPos + QCursor::pos() - s_pressPos);
        break;
    }
    return false;
}

void VLaunchWidget::setMainWidget(QWidget* pWidget)
{
    m_pLoginWidget->hide();
    m_pRegistWidget->hide();
    m_pReviseWidget->hide();
   
    m_pMainWidget = pWidget;
    m_pMainWidget->show();
}


void VLaunchWidget::initLabelTitle()
{
    m_pLabelTitle->setText("微信");
    m_pLabelTitle->setFont(QFont("微软雅黑", 10));
    m_pLabelTitle->setStyleSheet(R"(
        QLabel
        {
            color:#A0A0A0;  
        }
     )");
    m_pLabelTitle->move(8, 8);
}